home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16.lha / Python-1.6 / Lib / Python1.6 / nturl2path.py < prev    next >
Encoding:
Python Source  |  2000-02-04  |  1.8 KB  |  65 lines

  1. """Convert a NT pathname to a file URL and vice versa."""
  2.  
  3. def url2pathname(url):
  4.     """ Convert a URL to a DOS path...
  5.         ///C|/foo/bar/spam.foo
  6.  
  7.             becomes
  8.  
  9.         C:\foo\bar\spam.foo
  10.     """
  11.     import string, urllib
  12.     if not '|' in url:
  13.         # No drive specifier, just convert slashes
  14.         if url[:4] == '////':
  15.             # path is something like ////host/path/on/remote/host
  16.             # convert this to \\host\path\on\remote\host
  17.             # (notice halving of slashes at the start of the path)
  18.             url = url[2:]
  19.         components = string.split(url, '/')
  20.         # make sure not to convert quoted slashes :-)
  21.         return urllib.unquote(string.join(components, '\\'))
  22.     comp = string.split(url, '|')
  23.     if len(comp) != 2 or comp[0][-1] not in string.letters:
  24.         error = 'Bad URL: ' + url
  25.         raise IOError, error
  26.     drive = string.upper(comp[0][-1])
  27.     components = string.split(comp[1], '/')
  28.     path = drive + ':'
  29.     for  comp in components:
  30.         if comp:
  31.             path = path + '\\' + urllib.unquote(comp)
  32.     return path
  33.  
  34. def pathname2url(p):
  35.     """ Convert a DOS path name to a file url...
  36.         C:\foo\bar\spam.foo
  37.  
  38.             becomes
  39.  
  40.         ///C|/foo/bar/spam.foo
  41.     """
  42.  
  43.     import string, urllib
  44.     if not ':' in p:
  45.         # No drive specifier, just convert slashes and quote the name
  46.         if p[:2] == '\\\\':
  47.             # path is something like \\host\path\on\remote\host
  48.             # convert this to ////host/path/on/remote/host
  49.             # (notice doubling of slashes at the start of the path)
  50.             p = '\\\\' + p
  51.         components = string.split(p, '\\')
  52.         return urllib.quote(string.join(components, '/'))
  53.     comp = string.split(p, ':')
  54.     if len(comp) != 2 or len(comp[0]) > 1:
  55.         error = 'Bad path: ' + p
  56.         raise IOError, error
  57.  
  58.     drive = urllib.quote(string.upper(comp[0]))
  59.     components = string.split(comp[1], '\\')
  60.     path = '///' + drive + '|'
  61.     for comp in components:
  62.         if comp:
  63.             path = path + '/' + urllib.quote(comp)
  64.     return path
  65.